package net.sapfree.display.tags.course;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;


import net.sapfree.common.Constants;
import net.sapfree.core.db.DBConnectionProvider;
import net.sapfree.core.db.DataBaseExecutor;
import net.sapfree.core.db.Record;
import net.sapfree.core.db.RecordSet;
import net.sapfree.display.tags.core.LoopTagSupport;

public class CourseListTag extends LoopTagSupport {
	private static Logger log = Logger.getLogger(CourseListTag.class.getClass());
	private RecordSet courseList=null;
	private String quanuuid;
	private int count=-1;
	@Override
	protected boolean hasNext() throws JspException {
		return (courseList != null)&&(step<end);
	}

	@Override
	protected Object next() throws JspException {
		
		Record r=courseList.get(step);
		step++;
		return r;
	}

	 
	
	@Override
	protected void prepare() throws JspException {
		ServletRequest request = pageContext.getRequest();
		
		Connection conn =null;
		try {
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SAPFREE);
			DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
			if(quanuuid!=null&&!"".equals(quanuuid)){
				//根据班级uuid获取班级课程列表
				courseList=dbe.find(getCourseList(quanuuid));
			}
			
			pageContext.setAttribute("CourseListTag", this, PageContext.PAGE_SCOPE);
			end=courseList.size();
		 	step=0;
		} catch (SQLException e) {
			log.error("获取课程列表出错",e);
		}finally{
			try{
				 conn.close();
			}catch(Exception e){}
		}
	}
	
	
	
	public static String getCourseList(String quanuuid)
    {
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("SELECT * from COURSEDETAIL,COURSESCHEDULE where CS_UUID=CD_CSUUID AND CS_CLASSUUID='"+quanuuid+"'");

        return sBuffer.toString();
    }
	
	/**
	 * @return the filterName
	 */
	public String getQuanuuid() {
		return quanuuid;
	}

	/**
	 * @param filterName the filterName to set
	 */
	public void setQuanuuid(String quanuuid) {
		this.quanuuid = quanuuid;
	}
	
	/**
	 * @return the count
	 */
	public int getCount() {
		return count;
	}

	/**
	 * @param count the count to set
	 */
	public void setCount(int count) {
		this.count = count;
	}



}



